package Tests.MapModel;

import harima.agamengine.mapmodel.FileReference;
import harima.agamengine.mapmodel.MapModel;
import harima.agamengine.mapmodel.ReferenceType;
import harima.agamengine.mapmodel.TileModel;

import java.awt.Dimension;
import java.util.Random;

import junit.framework.TestCase;

public class MapModelFixture extends TestCase {

	Random random;

	protected void setUp() throws Exception {
		random = new Random();
	}

	public void test_CreatesTileData_MapIsCreated() throws Exception {
		int width = random.nextInt(100) + 1;
		int height = random.nextInt(100) + 1;

		MapModel model = new MapModel(new Dimension(width, height), "");

		TileModel[][] data = model.getData();

		assertTrue(data != null);
		assertEquals(width, data.length);
		assertEquals(height, data[0].length);
	}

	public void test_CreatesUnitLengthData_ModelIsCreated_DimensionHasNegativeValues() throws Exception {
		int width = random.nextInt(100) + 1;
		int height = random.nextInt(100) + 1;

		Dimension dimension = new Dimension(-width, -height);
		MapModel model = new MapModel(dimension, "");
				
		TileModel[][] data = model.getData();
		assertNotNull(data);
		assertEquals(1, data[0].length);
		assertEquals(1, data.length);
	}
	
	public void test_CanGetSetTileData() throws Exception {
		int width = 100;
		int height = 100;
		MapModel model = new MapModel(new Dimension(width, height), "");
		int x = random.nextInt(height);
		int y = random.nextInt(width);
	
		TileModel tile = new TileModel(1,false);
		
		assertNull(model.getTile(x, y));
		model.setTile(tile, x, y);
		assertSame(tile, model.getTile(x, y));
	}
	
	public void test_CanValidateTilePosition() throws Exception {
		MapModel model = new MapModel(new Dimension(1, 1), "");
		assertTrue( model.isInMap(0,0));
		assertFalse(model.isInMap(1, 2));
		assertFalse(model.isInMap(2, 1));
		assertFalse(model.isInMap(-1, 0));
		assertFalse(model.isInMap(0,-1));
	}
	
	public void test_ReturnsNull_GetTileRequested_PositionIsNotInMap() throws Exception {
		MapModel model = new MapModel(new Dimension(1, 1), "");
		assertNull(model.getTile(-1, 0));
		assertNull(model.getTile(0, 1));
		assertNull(model.getTile(0, -1));
		assertNull(model.getTile(1, 0));
		
	}
	
	public void test_ValidatesTilePosition_SetTileRequested() throws Exception {
		MapModel model = new MapModel(new Dimension(1, 1), "");
		model.setTile(new TileModel(1, false), -1, 0);
		model.setTile(new TileModel(1, false), 0, 1);
		model.setTile(new TileModel(1, false), 1, 0);
		model.setTile(new TileModel(1, false), 0, -1);	
	}
	
	public void test_CanAddFileReferfences() throws Exception {
		MapModel model = new MapModel(new Dimension(1, 1), "");
		String name = "Menu Image";
		String value = "menu.jpg";
		FileReference reference = new FileReference(name, value, ReferenceType.ImageFile);
		model.addFileReference(reference);
		FileReference recovered = model.getFileReference(name);
		assertNotNull(recovered);
		assertSame(reference, recovered);
		assertEquals(name, reference.getName());
		assertEquals(value, recovered.getValue());
		assertEquals(ReferenceType.ImageFile, recovered.getType());
	}
	
	public void test_CreatesDefaultTileZize_MapIsCreated_Always() throws Exception {
		MapModel model = new MapModel(new Dimension(), "");
		Dimension tileSize = model.getTileSize();
		assertNotNull(tileSize);
		assertEquals(32, tileSize.height);
		assertEquals(32, tileSize.width);
	}
}
